package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)" )
    public void autoFillPointcut(){};

    @Before("autoFillPointcut()")
    public void beforeAutoFillHandler(JoinPoint joinPoint){
        // 拿到操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();
        //未指定操作类型直接不执行
        if(operationType==null){
            return;
        }
        //拿到参数数据 一般都以第一个参数为Object对象
        Object arg = joinPoint.getArgs()[0];
        //创建修改的时间和修改的用户id
        LocalDateTime now = LocalDateTime.now();
        Long id = BaseContext.getCurrentId();

        //公共参数操作类型进行实现
        if(operationType==OperationType.INSERT){
            //插入进行公共参数填充
            try {
                // 获得该公共参数的set 方法进行设置值
                Method setCreatTime = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_USER,Long.class);
                Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);

                // 通过反射进行赋值公共参数
                setCreatTime.invoke(arg,now);
                setCreateUser.invoke(arg,id);
                setUpdateTime.invoke(arg,now);
                setUpdateUser.invoke(arg,id);


            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }else if(operationType==OperationType.UPDATE){
            //更新进程公共参数填充
            try {
                // 获得该公共参数的set 方法进行设置值

                Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);


                // 通过反射进行赋值公共参数
                setUpdateTime.invoke(arg,now);
                setUpdateUser.invoke(arg,id);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
